﻿using Deployment.Data.Builder;
using Deployment.Data.UI.Dialogs.Deploy;
using Deployment.Data.UI.Dialogs.Environment;
using Deployment.Data.UI.Dialogs.Variable;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Deployment.Data.UI.Controls.Builder
{
    /// <summary>
    /// Interaction logic for BuilderControl.xaml
    /// </summary>
    public partial class BuilderControl : UserControl
    {
        public BuilderControl()
        {
            InitializeComponent();
            this.DataContext = new DeploymentBuilder();
        }

        private void environmentBrowse_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "Connections.xml"; // Default file name
            dlg.DefaultExt = ".xml"; // Default file extension
            dlg.Filter = "XML documents|*.xml|All files|*.*"; // Filter files by extension

            // Show save file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process save file dialog box results
            if (result == true)
            {
                // Save document
                this.txt_EnvironmentsFile.Text = dlg.FileName;
            }
        }

        private void environmentEdit_Click(object sender, RoutedEventArgs e)
        {
            EnvironmentEditorWindow dlg = new EnvironmentEditorWindow(txt_EnvironmentsFile.Text);
            dlg.Owner = Window.GetWindow(this);
            
            dlg.ShowDialog();
            if (string.IsNullOrEmpty(this.txt_EnvironmentsFile.Text))
                this.txt_EnvironmentsFile.Text = dlg.EnvironmentsFile;
        }

        private void deploymentBrowse_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "Deployment.xml"; // Default file name
            dlg.DefaultExt = ".xml"; // Default file extension
            dlg.Filter = "XML documents|*.xml|All files|*.*"; // Filter files by extension

            // Show save file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process save file dialog box results
            if (result == true)
            {
                // Save document
                this.txt_deploymentfile.Text = dlg.FileName;
            }
        }

        private void deploymentEdit_Click(object sender, RoutedEventArgs e)
        {
            DeploymentBuilderDialog dlg = new DeploymentBuilderDialog(txt_deploymentfile.Text);
            if (!string.IsNullOrEmpty(txt_EnvironmentsFile.Text))
            {
                Data.Environment.EnvironmentsFile envFile = Data.Environment.EnvironmentsFile.LoadFromFile(txt_EnvironmentsFile.Text);
                dlg.Environments = envFile;
            }
            dlg.Owner = Window.GetWindow(this);
            dlg.ShowDialog();
            if (string.IsNullOrEmpty(txt_deploymentfile.Text))
                this.txt_deploymentfile.Text = dlg.DeploymentFile;
        }

        private void variableBrowse_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "Variables.xml"; // Default file name
            dlg.DefaultExt = ".xml"; // Default file extension
            dlg.Filter = "XML documents|*.xml|All files|*.*"; // Filter files by extension

            // Show save file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process save file dialog box results
            if (result == true)
            {
                // Save document
                this.txt_deploymentfile.Text = dlg.FileName;
            }
        }

        private void variableEdit_Click(object sender, RoutedEventArgs e)
        {
            VariablesDialog dlg = new VariablesDialog(txt_VariableFile.Text);
            if (!string.IsNullOrEmpty(txt_EnvironmentsFile.Text))
            {
                Data.Environment.EnvironmentsFile envFile = Data.Environment.EnvironmentsFile.LoadFromFile(txt_EnvironmentsFile.Text);
                dlg.Environments = envFile;
            }
            dlg.Owner = Window.GetWindow(this);
            dlg.ShowDialog();
            if (string.IsNullOrEmpty(txt_VariableFile.Text))
                this.txt_VariableFile.Text = dlg.VariablesFilePath;
        }
    }
}
